package com.zxy.salenet.web.service.imple;

import com.zxy.salenet.web.mapper.SalenetAdminsMapper;
import com.zxy.salenet.web.mapper.SalenetUserMapper;
import com.zxy.salenet.web.pojo.SalenetAdmins;
import com.zxy.salenet.web.pojo.SalenetUser;
import com.zxy.salenet.web.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.Date;
import java.util.Random;

/**
 * @author: zxy
 * @version: 1.0
 * @description: 登陆注册接口实现类
 */
 /**
 * LoginServiceImpl
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    private SalenetUserMapper userMapper;
    @Autowired
    private SalenetAdminsMapper adminMapper;
    /**
     * 加密盐
     */
    private String salt = "zxy";

    /**
     * 用户注册
     *
     * @param salenetUser 注册的用户信息
     * @return 是否注册成功 0 | 1
     */
    @Override
    public Integer addUser(SalenetUser salenetUser) {
        //添加用户需要先进行查询在进行插入
        //随机生成用户唯一id
        Integer userUnionId = new Random().nextInt(100000) + 1000;
        salenetUser.setUserId(userUnionId);
        //如果存在一个相同的用户  判定  4 用户名重复  5 手机号重复 6 真实姓名重复
        //     userid如果重复则进行重新随机
        Integer userIdResult = userMapper.selectRepUserId(salenetUser.getUserId());
        if (userIdResult > 0) {
            userUnionId = new Random().nextInt(100000) + 1000 + new Random().nextInt(900);
            salenetUser.setUserId(userUnionId);
        }
        //用户名重复
        Integer userNameResult = userMapper.selectRepName(salenetUser.getUserName());
        if (userNameResult > 0) {
            return 4;
        }
        //真实姓名重复
        Integer userRealNameResult = userMapper.selectRepRealName(salenetUser.getUserRealName());
        if (userRealNameResult > 0) {
            return 6;
        }
        //手机号重复
        Integer userPhoneResult = userMapper.selectRepPhone(salenetUser.getUserPhone());
        if (userPhoneResult > 0) {
            return 5;
        }
        //加密密码
        salenetUser.setUserPasswd(DigestUtils.md5DigestAsHex((salt + salenetUser.getUserPasswd()).getBytes()));
        //加密支付密码
        salenetUser.setUserPaypass(DigestUtils.md5DigestAsHex((salt + salenetUser.getUserPaypass()).getBytes()));
        //设置默认收货地址为空
        salenetUser.setUserComm(0);
        System.out.println(salenetUser.toString());
        Integer insertResult = userMapper.insertUsers(salenetUser);
        return insertResult;
    }

    /**
     * 用户登陆
     *
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @Override
    public SalenetUser userLogin(String userName, String password) {
        //MD5加密不可逆  所以这里暂时使用对前端发送过来的数据也进行加密 然后进行判断  后期寻找解密算法
        String cryPassword = DigestUtils.md5DigestAsHex((salt + password).getBytes());
        System.out.println("测试");
        //进行判断登陆
        String phone = userName;
        System.out.println("账号" + userName + "密码" + cryPassword + "原密码" + password);
        SalenetUser salenetUser = userMapper.selectUserLoginByUserNameAndPassword(userName, phone, cryPassword);
        return salenetUser;
    }

    /**
     * 管理员登陆
     *
     * @param adminsName
     * @param adminsPasswd
     * @return
     */
    @Override
    public SalenetAdmins adminsLogin(String adminsName, String adminsPasswd) {
        adminsPasswd = salt+adminsPasswd;
        System.out.println("加密密码"+adminsPasswd);
        SalenetAdmins admin = adminMapper.selectAdminByUserNameAndPasswd(adminsName, DigestUtils.md5DigestAsHex(adminsPasswd.getBytes()));
        return admin;
    }

    @Override
    public void test() {
        System.out.println("测试");
    }
}
